#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda_pytorch.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/init_xdg_cache.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/comfyui/env"

echo "lmdprogress:5"

export APP_INSTALL_NAME="comfyui"
export CURRENT_APP_PATH="${LMD_APPS_DIR}/ComfyUI"
echo $CURRENT_APP_PATH
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:15"
if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
# clone and fetch ComfyUI
export GIT_FETCH_ALL_HISTORY="true"
export GIT_REPO_URL="${GIT_HOST}/comfyanonymous/ComfyUI"
export GIT_REPO_BRANCH="master"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:30"


cd "$CURRENT_APP_PATH"
export CONDA_PREFIX="./venv"

export PYTHON_VER_ENV=3.12
if conda run -p $CONDA_PREFIX --no-capture-output pip --version > /dev/null 2>&1; then
  echo "venv works."
else
    echo "venv dose not work. create venv."
    if [ -d "$CONDA_PREFIX" ]; then
        echo "found ./venv. remove it. "
        rm -rf "$CONDA_PREFIX"
    fi
    conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y
fi

echo "lmdprogress:35"

conda_run_pip_install huggingface_hub hf_xet safetensors watchdog modelscope

source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_flash_attn.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_nunchaku.sh"

# conda_run_cmd -p $CONDA_PREFIX --no-capture-output pip install -r requirements.txt

# echo "lmdprogress:40"
# conda run -p $CONDA_PREFIX --no-capture-output pip install safetensors watchdog
echo "lmdprogress:45"

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    # conda_run_pip_install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    if [ "$ARCH" = "x86_64" ]; then
        conda_run_conda_forge_install av
    fi
elif current_os_is_windows; then
    # conda run -p $CONDA_PREFIX --no-capture-output pip uninstall torch torchvision torchaudio 
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
fi

echo "lmdprogress:50"

conda_run_pip_install -r requirements.txt
echo "lmdprogress:60"

echo 'Install ComfyUI-Manager'
cd "${CURRENT_APP_PATH}/custom_nodes"
export COMFYUI_MANAGER_DIR="${CURRENT_APP_PATH}/custom_nodes/ComfyUI-Manager"

# clone and fetch ComfyUI-Manager
export GIT_REPO_URL="${GIT_HOST}/ltdrdata/ComfyUI-Manager"
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$COMFYUI_MANAGER_DIR"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:95"

cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/run-cm-cli.py" "$COMFYUI_MANAGER_DIR"
cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/aiohttp_proxy.py" "$COMFYUI_MANAGER_DIR"
# if [ -n "$_INSTALL_SUCCESS_LOG" ]; then
#     echo $_INSTALL_SUCCESS_LOG
# fi

echo "lmdprogress:100"